package com.ruoyi.system.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AcIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AcWorkOrderMapper;
import com.ruoyi.system.domain.AcWorkOrder;
import com.ruoyi.system.service.IAcWorkOrderService;
import redis.clients.jedis.Jedis;

/**
 * 保养维护工单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class AcWorkOrderServiceImpl implements IAcWorkOrderService
{
    @Autowired
    private AcWorkOrderMapper acWorkOrderMapper;


    /**
     * 查询保养维护工单
     *
     * @param id 保养维护工单主键
     * @return 保养维护工单
     */
    @Override
    public AcWorkOrder selectAcWorkOrderById(Long id)
    {
        return acWorkOrderMapper.selectAcWorkOrderById(id);
    }


    @Override
    public AcIndex getIndexInfo(AcIndex acIndex)
    {
        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }
        return acWorkOrderMapper.getIndexInfo( acIndex);
    }

    @Override
    public List<AcIndex>  getAssetStatus(AcIndex acIndex)
    {
        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }
        return acWorkOrderMapper.getAssetStatus(acIndex);
    }

    @Override
    public List<AcIndex>  getWorkOrderStatus(AcIndex acIndex)
    {
        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }

        return acWorkOrderMapper.getWorkOrderStatus(acIndex);
    }

    @Override
    public List<AcIndex>  getContractStatus(AcIndex acIndex)
    {

        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }
        return acWorkOrderMapper.getContractStatus(acIndex);
    }

    @Override
    public List<AcIndex>  getContractEnd(AcIndex acIndex)
    {
        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }
        return acWorkOrderMapper.getContractEnd(acIndex);
    }

    @Override
    public List<AcIndex>  getWorkOrder(AcIndex acIndex)
    {
        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }
        return acWorkOrderMapper.getWorkOrder(acIndex);
    }

    @Override
    public List<AcIndex>  getContractRent(AcIndex acIndex)
    {
        if ( acIndex.getDeptId() == null){
            acIndex.setDeptId(SecurityUtils.getDeptId());
        }
        return acWorkOrderMapper.getContractRent(acIndex);
    }



    @Override
    public List<AcWorkOrder>  warnTask()
    {
        AcWorkOrder acWorkOrder = new AcWorkOrder();
        List<AcWorkOrder> list =   acWorkOrderMapper.selectAcWorkOrderList(acWorkOrder);
//        WebSocketUsers.sendMessageToUsersByText(JSON.toJSONString(list));
      return list;
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderNum() {
        return acWorkOrderMapper.selectWorkOrderNum();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderProportion() {
        return  acWorkOrderMapper.selectWorkOrderProportion();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderProcessingSpeed() {
        return  acWorkOrderMapper.selectWorkOrderProcessingSpeed();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderType() {
        return  acWorkOrderMapper.selectWorkOrderType();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderStatus() {
        return  acWorkOrderMapper.selectWorkOrderStatus();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderUrgency() {
        return  acWorkOrderMapper.selectWorkOrderUrgency();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderAccumulatedTime() {
        return  acWorkOrderMapper.selectWorkOrderAccumulatedTime();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderInspectionFeedback() {
        return  acWorkOrderMapper.selectWorkOrderInspectionFeedback();
    }

    @Override
    public List<AcWorkOrder> selectWorkOrderProcessingRank() {
        return acWorkOrderMapper.selectWorkOrderProcessingRank();
    }

    /**
     * 查询保养维护工单列表
     *
     * @param acWorkOrder 保养维护工单
     * @return 保养维护工单
     */
    @Override
    public List<AcWorkOrder> selectAcWorkOrderList(AcWorkOrder acWorkOrder)
    {
        acWorkOrder.setDeptId(SecurityUtils.getDeptId());
        return acWorkOrderMapper.selectAcWorkOrderList(acWorkOrder);
    }

    /**
     * 新增保养维护工单
     *
     * @param acWorkOrder 保养维护工单
     * @return 结果
     */
//    @Override
//    public int insertAcWorkOrder(AcWorkOrder acWorkOrder)
//    {
//        acWorkOrder.setCreateTime(DateUtils.getNowDate());
//        return acWorkOrderMapper.insertAcWorkOrder(acWorkOrder);
//    }


//    新增工单的时候将数据 同步到redis中
    @Override
    public int insertAcWorkOrder(AcWorkOrder acWorkOrder) {
        acWorkOrder.setDeptId(SecurityUtils.getDeptId());
        int result = acWorkOrderMapper.insertAcWorkOrder(acWorkOrder);
//        if (result > 0) {
//            // 同步到redis中
//            Map<String, Object> resultMap = new LinkedHashMap<>(8);
//            try {
//                Jedis jedis = new Jedis("127.0.0.1", 6379);
//                jedis.select(6);
//// 将 acWorkOrder 转成 json 字符串
//                String json = JSON.toJSONString(acWorkOrder);
//                Map<Long, AcWorkOrder> map = new HashMap<>();
//                map.put(acWorkOrder.getId(), acWorkOrder);
//// map 转为字符串
////                String jsonMap = JSON.toJSONString(json);
//                List<String> assetWorkOrderList = new ArrayList<>(); // 创建一个 Java 的 List 对象
//                try {
//                    if (jedis.exists("assetWorkOrder")) {
//                        // 如果 assetWorkOrder 列表已经存在，将 jsonMap 添加到列表的末尾
//                        jedis.rpush("assetWorkOrder", json);
//                        // 从 Redis 中获取列表所有元素
////                        assetWorkOrderList = jedis.lrange("assetWorkOrder", 0, -1);
//                    } else {
//                        // 如果 assetWorkOrder 列表不存在，创建一个新的列表并将 jsonMap 作为第一个元素
//                        jedis.lpush("assetWorkOrder", json);
//                        assetWorkOrderList.add(json);
//                    }
//                } catch (Exception e) {
//                    // 处理 Redis 操作异常
//                    System.err.println("Failed to add data to Redis: " + e.getMessage());
//                } finally {
//                    // 关闭连接
//                    if (jedis != null) {
//                        jedis.close();
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
            return result;

    }



    /**
     * 修改保养维护工单
     *
     * @param acWorkOrder 保养维护工单
     * @return 结果
     */
    @Override
    public int updateAcWorkOrder(AcWorkOrder acWorkOrder)
    {
        acWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return acWorkOrderMapper.updateAcWorkOrder(acWorkOrder);
    }

    /**
     * 批量删除保养维护工单
     *
     * @param ids 需要删除的保养维护工单主键
     * @return 结果
     */
    @Override
    public int deleteAcWorkOrderByIds(Long[] ids)
    {
        return acWorkOrderMapper.deleteAcWorkOrderByIds(ids);
    }

    /**
     * 删除保养维护工单信息
     *
     * @param id 保养维护工单主键
     * @return 结果
     */
    @Override
    public int deleteAcWorkOrderById(Long id)
    {
        return acWorkOrderMapper.deleteAcWorkOrderById(id);
    }
}
